package com.neudu.ssrs_yyzxservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.ssrs_yyzxservice.common.JSONReturn;
import com.neudu.ssrs_yyzxservice.mapper.OutwardMapper;
import com.neudu.ssrs_yyzxservice.pojo.Outward;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class OutwardController {

    @Autowired
    private OutwardMapper outwardMapper;

    @Autowired
    private JSONReturn jsonReturn;

    // 查询所有外出记录（分页）
    @PostMapping("/get_all_outwards")
    public String getAllOutwards(@RequestBody PageInfo pageInfo) {
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            List<Outward> outwardList = outwardMapper.selectList(null);
            PageInfo<Outward> pageInfoResult = new PageInfo<>(outwardList);
            return jsonReturn.returnSuccess(pageInfoResult);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 根据客户ID查询外出记录
    @PostMapping("/get_outwards_by_customer_id")
    public String getOutwardsByCustomerId(@RequestBody Outward outward) {
        try {
            LambdaQueryWrapper<Outward> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Outward::getCustomerId, outward.getCustomerId());
            List<Outward> outwardList = outwardMapper.selectList(queryWrapper);
            return jsonReturn.returnSuccess(outwardList);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 添加一条外出记录
    @PostMapping("/add_one_outward")
    public String addOutward(@RequestBody Outward outward) {
        try {
            int result = outwardMapper.insert(outward);
            if (result > 0) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 根据ID查询外出记录
    @PostMapping("/get_outward_by_id")
    public String getOutwardById(@RequestParam("id") Integer id) {
        try {
            Outward outward = outwardMapper.selectById(id);
            return jsonReturn.returnSuccess(outward);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 修改外出记录
    @PostMapping("/modify_outward_by_id")
    public String modifyOutwardById(@RequestBody Outward outward) {
        try {
            int result = outwardMapper.updateById(outward);
            if (result > 0) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 删除外出记录
    @PostMapping("/remove_outward_by_id")
    public String removeOutwardById(@RequestParam("id") Integer id) {
        try {
            int result = outwardMapper.deleteById(id);
            if (result > 0) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}